package com.ys.openapi.utils;

import com.alibaba.fastjson.JSON;
import com.erp.utils.HttpUtils;
import com.erp.utils.PropertiesUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ys.openapi.organization.entity.DepartmentEntity;
import okhttp3.Headers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 云枢工具类
 */
public class ClientUtils  {
    private final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    private Client apiClient = new Client(false);
    private  static String openApiURL="";
    private static  String accessTokenUrl="";
     static {
         Properties ftp = PropertiesUtils.loadProperties("openapi");
          String clientSecret=ftp.getProperty("clientSecret");
          String clientId=ftp.getProperty("clientId");
          String url=ftp.getProperty("url");
         openApiURL=ftp.getProperty("path");
         accessTokenUrl= url+clientId+"&client_secret="+clientSecret;
     }
    /**
     * get方式调用 api
     * @param path  相对路径
     * @return Object对象
     * @throws Exception
     */
    public  Object get(String path)throws  Exception{
        JsonObject object=getHeader();
        path=openApiURL+path;
        String r = "";//apiClient.get(path, Headers.of("access_token", object.get("access_token").getAsString()), queryParams);
        r=HttpUtils.doGet(path,"access_token",object.get("access_token").getAsString());
        Object parse = JSON.parse(r);
        return parse;
    }
    public  String getString(String path, Map<String, String> queryParams)throws  Exception {
        JsonObject object=getHeader();
        path=openApiURL+path;
        String r = apiClient.get(path, Headers.of("access_token", object.get("access_token").getAsString()), queryParams);
        return r;
    }

    /**
     * json串转javaBean
     * @param json
     * @param clazz
     * @param <T>
     * @return
     * @throws Exception
     */
    public <T> T parstObject(String json, Class<T> clazz)throws Exception{
        return  gson.fromJson(json, clazz);
    }

    /**
     * post方式调用api
     * @param path 相对路径
     * @param queryParams  普通参数请用QueryParam，
     * @param body 复杂对象参数请使用json传参
     * @return
     */
    public Object post(String path,Map<String, String> queryParams,Object body)throws Exception{
        JsonObject object=getHeader();
        path=openApiURL+path;
       String r="";
       r= HttpUtils.doPost(path,"access_token",object.get("access_token").getAsString(),body.toString());
        Object parse = JSON.parse(r);
        return parse;
    }
    public String postString(String path,Map<String, String> queryParams,Object body)throws Exception{
        JsonObject object=getHeader();
        path=openApiURL+path;
        String r = apiClient.post(path,Headers.of("access_token", object.get("access_token").getAsString()),queryParams,body);
        return r;
    }
    /**
     * 获取accessToken
     * @return
     */
    private JsonObject getHeader()throws Exception{
        String accessToken = apiClient.post(accessTokenUrl, (Headers) null, null, null);
        JsonObject object = new JsonParser().parse(accessToken).getAsJsonObject();
        return object;
    }
    public static void main(String[] args) throws Exception {
       /* Client apiClient = new Client(false);
        String clientSecret="0a417ecce58c31b32364ce19ca8fcd15";
        String clientId="xclient";
        String url="http://47.100.197.8:8099/oauth/token?grant_type=client_credentials&client_id="+clientId+"&client_secret="+clientSecret;
        String accessToken = apiClient.post(url, (Headers) null, null, null);
        JsonObject object = new JsonParser().parse(accessToken).getAsJsonObject();
        String r = apiClient.get("http://47.100.197.8:8099/api/v1/app/getAllPublishBizSchema", Headers.of("access_token", object.get("access_token").getAsString()), null);
        System.out.println(r);
        JSON.parse(r);
        JsonObject obj= new JsonParser().parse(r).getAsJsonObject();*/
        ClientUtils utils=new ClientUtils();
      /*  Object object = utils.get("app/getAllPublishBizSchema", null);
        System.out.println(object);
        //新增部门​/api​/v1​/organization​/addDepartment
        List<String> list=new ArrayList<>();
        list.add("2c2c80867a1f88d8017a5d892a2610ea");
        Object post = utils.post("organization/getDepartmentsChildList", null, list);
        System.out.println(post);*/
       /* Map<String, String> queryParams=new HashMap<>();
        List<String> list=new ArrayList<>();
        list.add("2c2c80867a1f88d8017a5d892a2610ea");
        queryParams.put("ids",list.toString());
        Object o = utils.get("organization/getDepartmentsChildList", queryParams);
        System.out.println(o);*/
        Map<String, String> queryParams=new HashMap<>();
        queryParams.put("parentId","2c2c80867a1f88d8017a58572f9309fd");
        queryParams.put("name","测试333444");
        queryParams.put("deptType","YUNS_DEPT");
        queryParams.put("queryCode","1_1#CP210717444444444444");
        queryParams.put("sourceid","CP21071777788");
        queryParams.put("corpId","main");
        queryParams.put("leaf","true");

        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        DepartmentEntity de=new DepartmentEntity();
        de.setCorpId("main");
        de.setDeptType("YUNS_DEPT");
        de.setLeaf(true);
        de.setName("测试王王王五");
        de.setSourceid("CP"+sdf.format(new Date())+ Calendar.getInstance().get(Calendar.MILLISECOND));
        de.setQueryCode("1_1#"+de.getSourceid());
        de.setParentId("2c2c80867a1f88d8017a58572f9309fd");
        System.out.println(queryParams.toString());
        System.out.println(JSON.toJSONString(de));

    }
}
